package 第3周_数据结构基础.第2章栈和队列.Queue;

public class TestQueue {

    private Integer[] data;
    //队列首和队列尾
    private Integer front, tail;
    //队列里面的元素
    private Integer size;

    public TestQueue(int k) {
        data = new Integer[k];
        front = 0;
        tail = 0;
        size = 0;
    }

    public Integer getCapacity(){
        return data.length;
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        data[tail] = value;
        tail = (tail + 1) % data.length;
        size++;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        Integer ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        return true;
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return data[front];
    }

    public Integer Rear() {
        if(isEmpty()){
            return -1;
        }
        return data[(tail + data.length - 1) % data.length];
    }

    public boolean isEmpty() {
        return front == tail;
    }

    public boolean isFull() {
        return size == data.length;
    }




    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue：size = %d, capacity = %d\n", size, getCapacity()));
        res.append("front [");

        for (int i = front; i != tail; i  = (i + 1) % data.length) {
            res.append(data[i]);
            if ((i + 1) % data.length != tail){
                res.append(", ");
            }
        }
        res.append("] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        TestQueue queue = new TestQueue(10);
        for (int i = 0; i < 10; i++) {
            queue.enQueue(i);
            System.out.println(queue);

            if (i % 3 == 2){
                queue.deQueue();
                System.out.println(queue);
            }
            System.out.println(queue.Rear());
        }


    }
}
